package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.properties.JwtProperties;
import com.sky.service.ShopingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShopingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long id = BaseContext.getCurrentId();
        //判断购物车是否有该数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getByUser(id, shoppingCartDTO);
        if (shoppingCarts != null && shoppingCarts.size() > 0) {//有数据
            ShoppingCart shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
        } else {//添加购物车
            ShoppingCart shoppingCart = new ShoppingCart();
            //查询信息
            if (shoppingCartDTO.getDishId() != null) {
                Dish dish = dishMapper.getbyId(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(dish.getId());
                if (shoppingCartDTO.getDishFlavor() != null) {
                    shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                }
            } else {
                Setmeal setmeal = setmealMapper.getByid(shoppingCartDTO.getSetmealId());
                shoppingCart.setSetmealId(setmeal.getId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setUserId(id);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> getShopCarts() {
        Long userid = BaseContext.getCurrentId();
        return shoppingCartMapper.getAll(userid);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanAll(userId);
    }

    @Override
    public void cleanBydto(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //判断是否为1
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> byUser = shoppingCartMapper.getByUser(BaseContext.getCurrentId(), shoppingCartDTO);
        if (byUser != null && byUser.size() > 0) {
            ShoppingCart shoppingCart1 = byUser.get(0);
            if (shoppingCart1.getNumber() == 1) { //数量为1
                if (setmealId != null) {//delete setmeal
                    shoppingCart.setSetmealId(setmealId);
                } else {
                    shoppingCart.setDishId(shoppingCartDTO.getDishId());
                    shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                }
                shoppingCartMapper.deleteByCon(shoppingCart);
            } else {//数量不为1
                //直接数量减1
                shoppingCart.setNumber(shoppingCart1.getNumber() - 1);
                shoppingCart.setId(shoppingCart1.getId());
                shoppingCartMapper.update(shoppingCart);
            }
        }

    }
}
